package com.myself.springboot.springboot_study.controller;

import com.myself.springboot.springboot_study.exception.MyLoginException;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class controllerTest {
    @RequestMapping("/bbb")
    public String hellowWorld () {
        return "666777www";
    }

    @RequestMapping("/thymeleafTest")
    public String test002 ( Map<String,Object> map) {
        map.put("hellow","这是thymleaf的方法");
        return "success";
    }

    @RequestMapping("/login")
    public String login () { return "login";}

    @PostMapping(value = "/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String,Object> map, HttpSession session){
        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
            //登陆成功，防止表单重复提交，可以重定向到主页
            if (username.equals("aa")){
                throw new MyLoginException();
            }
            session.setAttribute("username",username);
            return "redirect:/main.html";
        }else{
            //登陆失败

            map.put("msg","用户名密码错误");
            return  "login";
        }

    }


}


